<?php

/**
 * 生成真静态
 * 
 * @author ShuangYa
 * @package Blog
 * @category Library
 * @link http://www.sylingd.com/
 * @copyright Copyright (c) 2015 ShuangYa
 * @license http://lab.sylingd.com/go.php?name=blog&type=license
 */

namespace blog\libs;
use \Sy;
use \blog\libs\BHtml;
use \blog\libs\option;
use \blog\libs\BArticle;
use \blog\libs\BMeta;

class MkHtml {
	/**
	 * 检查支持情况
	 * @access public
	 * @return boolean
	 */
	public static function checkSupport() {
		if (!Sy::$app['rewrite']) {
			return FALSE;
		}
		return TRUE;
	}
	/**
	 * URL转本地目录
	 * @access public
	 * @param string $url
	 * @return string
	 */
	public static function url2local($url) {
		$path = parse_url($url, PHP_URL_PATH);
		$path = rtrim(Sy::$webrootDir, '/') . $path;
		return $path;
	}
	/**
	 * 通用
	 */
	public static function maker($url, $getContent, $getContentParam = '') {
		if (!static::checkSupport()) {
			return;
		}
		$path = static::url2local($url);
		$path_info = pathinfo($path);
		\blog\mkdirs($path_info['dirname']);
		ob_start();
		$getContent($getContentParam);
		$content = ob_get_clean();
		file_put_contents($path, $content);
	}
	/**
	 * 生成静态文件：文章
	 * @access public
	 * @param int $id
	 * @return boolean
	 */
	public static function mkArticle($id) {
		$article = BArticle::get($id);
		$url = Sy::createUrl(['article/view', 'id' => $id]);
		static::maker($url, function ($article) {
			Sy::view('article/view', ['article' => $article]); }
		, $article);
	}
	/**
	 * 生成静态文件：Feed
	 * @access public
	 * @param string $type
	 */
	public static function mkFeed($type) {
		$a = 'action' . ucfirst($type);
		if (!class_exists('\CFeed', FALSE)) {
			require (Sy::$appDir . 'controllers/feed.php');
		}
		$url = Sy::createUrl('feed/' . $type, 'xml');
		$path = static::url2local($url);
		$path_info = pathinfo($path);
		\blog\mkdirs($path_info['dirname']);
		$feed = new \CFeed(FALSE, $path);
		if (!method_exists($feed, $a)) {
			return;
		}
		$feed->$a();
	}
	/**
	 * 生成静态文件：Meta，逻辑太复杂，暂不编写
	 * @access public
	 * @param int $id
	 */
	public static function mkMeta($id) {
		$meta = BMeta::get($id);
	}
}
